# named colors
COLOR_RST = \e[0m
COLOR_ACT = \e[1;32m
COLOR_ARG = \e[1;35m

# build tools and flags
CC = clang++
LD = clang++

# debug flags -D DEBUGGING_ENABLED -g
CCFLAGS = -fno-operator-names -march=native -std=c++14 -Wall -Wextra -fopenmp -fsanitize=undefined -D DEBUGGING_ENABLED -O3
LDFLAGS = -fopenmp -fsanitize=undefined
OBJECTS = $(BUILD)/src/app/app.o $(BUILD)/src/kernel/no-atomic-pad.o $(BUILD)/src/kernel/no-atomic.o $(BUILD)/src/main.o $(BUILD)/src/utils/graph.o $(BUILD)/src/utils/painter.o $(BUILD)/src/utils/timer.o $(BUILD)/src/worker/worker.o
BUILD = build
BIN = $(BUILD)/build

# phonies
.PHONY: all clean test reconf rebuild
all: $(BIN)
clean:
	@echo -e "$(COLOR_ACT)removing $(COLOR_ARG)$(BUILD)$(COLOR_RST)..."
	rm -rf $(BUILD)/
test: all
	@echo -e "$(COLOR_ACT)running $(COLOR_ARG)$(BIN)$(COLOR_RST)..."
	$(BIN)
reconf:
	@echo -e "$(COLOR_ACT)reconfiguring$(COLOR_RST)..."
	./configure
rebuild: clean
	@$(MAKE) --no-print-directory all

# build rules
$(BUILD)/:
	@echo -e "$(COLOR_ACT)making directory $(COLOR_ARG)$(BUILD)/$(COLOR_RST)..."
	mkdir -p $(BUILD)/
$(BUILD)/src: | $(BUILD)/
	@echo -e "$(COLOR_ACT)making directory $(COLOR_ARG)$(BUILD)/src$(COLOR_RST)..."
	mkdir -p $(BUILD)/src
$(BUILD)/src/app: | $(BUILD)/src
	@echo -e "$(COLOR_ACT)making directory $(COLOR_ARG)$(BUILD)/src/app$(COLOR_RST)..."
	mkdir -p $(BUILD)/src/app
$(BUILD)/src/kernel: | $(BUILD)/src
	@echo -e "$(COLOR_ACT)making directory $(COLOR_ARG)$(BUILD)/src/kernel$(COLOR_RST)..."
	mkdir -p $(BUILD)/src/kernel
$(BUILD)/src/utils: | $(BUILD)/src
	@echo -e "$(COLOR_ACT)making directory $(COLOR_ARG)$(BUILD)/src/utils$(COLOR_RST)..."
	mkdir -p $(BUILD)/src/utils
$(BUILD)/src/worker: | $(BUILD)/src
	@echo -e "$(COLOR_ACT)making directory $(COLOR_ARG)$(BUILD)/src/worker$(COLOR_RST)..."
	mkdir -p $(BUILD)/src/worker
$(BUILD)/src/app/app.o: src/app/app.cc src/app/app.hh src/config.hh src/lib/cpptoml/cpptoml.hh src/meta/constraints.hh src/utils/debug.hh src/utils/graph.hh src/utils/painter.hh src/utils/units.hh src/worker/worker.hh third-party/cpptoml/cpptoml.hh | $(BUILD)/src/app
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/app/app.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/kernel/no-atomic-pad.o: src/kernel/no-atomic-pad.cc src/kernel/no-atomic-pad.hh src/kernel/utils.hh src/meta/constraints.hh src/utils/concurrent-queue.hh src/utils/debug.hh src/utils/graph.hh src/utils/heap.hh src/utils/impl.hh src/utils/painter.hh src/utils/timer.hh | $(BUILD)/src/kernel
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/kernel/no-atomic-pad.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/kernel/no-atomic.o: src/kernel/no-atomic.cc src/kernel/no-atomic.hh src/kernel/utils.hh src/meta/constraints.hh src/utils/concurrent-queue.hh src/utils/debug.hh src/utils/graph.hh src/utils/heap.hh src/utils/impl.hh src/utils/painter.hh src/utils/timer.hh | $(BUILD)/src/kernel
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/kernel/no-atomic.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/main.o: src/main.cc src/app/app.hh src/config.hh src/lib/cpptoml/cpptoml.hh third-party/cpptoml/cpptoml.hh | $(BUILD)/src
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/main.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/utils/graph.o: src/utils/graph.cc src/meta/constraints.hh src/utils/debug.hh src/utils/graph.hh src/utils/painter.hh src/utils/string.hh | $(BUILD)/src/utils
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/utils/graph.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/utils/painter.o: src/utils/painter.cc src/meta/constraints.hh src/utils/debug.hh src/utils/graph.hh src/utils/painter.hh | $(BUILD)/src/utils
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/utils/painter.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/utils/timer.o: src/utils/timer.cc src/utils/timer.hh | $(BUILD)/src/utils
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/utils/timer.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BUILD)/src/worker/worker.o: src/worker/worker.cc src/kernel/kernels.hh src/kernel/no-atomic-pad.hh src/kernel/no-atomic.hh src/kernel/utils.hh src/meta/constraints.hh src/utils/concurrent-queue.hh src/utils/debug.hh src/utils/graph.hh src/utils/heap.hh src/utils/impl.hh src/utils/painter.hh src/utils/units.hh src/worker/worker.hh | $(BUILD)/src/worker
	@echo -e "$(COLOR_ACT)compiling $(COLOR_ARG)src/worker/worker.cc$(COLOR_RST)..."
	$(CC) -c -o '$@' '$<' $(CCFLAGS)
$(BIN): $(OBJECTS) | $(BUILD)/
	@echo -e "$(COLOR_ACT)loading $(COLOR_ARG)build$(COLOR_RST)..."
	$(LD) -o '$@' $(OBJECTS) $(LDFLAGS)

